<script setup>
import { ref,reactive, onMounted } from 'vue'
import { useRoute } from 'vue-router';
import {update,find}  from '../../api/news.js'
import ImageUpload from '../../components/ImageUpload.vue'
import WangEditer from '@/components/WangEditer.vue';

const route = useRoute();
const state = reactive({
  form:{
    id:'',
    title:'',
    image:'',
    content:''
  }
 
});

onMounted(()=>{
  let id = route.query.id;
  find({id:id}).then((res)=>{
      // console.log(res)
      state.form = res.data
  })
});
const onSubmit = ()=>{
  update(state.form).then(()=>{

  })
}
const updateImageUrl = (imageUrl) => {
  console.log('imageUrl:',imageUrl);
  state.form.image = imageUrl;
}
const updateEditor =(content)=>{
  state.form.content = content;
}
</script>

<template>
  <el-col :span="16"><div class="grid-content ep-bg-purple-dark" />
         <el-form :model="state.form" label-width="120px">
            <el-form-item label="分类名">
              <el-input v-model="state.form.title" />
            </el-form-item>
              <el-form-item label="主图">
                <ImageUpload  :imageUrl="state.form.image" @update:imageUrl="updateImageUrl"/>
            </el-form-item>
            <el-form-item label="内容">
                  <WangEditer :htmlStr="state.form.content"  @update:htmlStr="updateEditor"/>
            </el-form-item>
            
          </el-form>
       <el-form-item>
        <el-button type="primary" @click="onSubmit">Create</el-button>
      </el-form-item>
    </el-col>
  
</template>

<style scoped>
.read-the-docs {
  color: #888;
}
</style>